package use.thread.sync.reentrantlock.aqs;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;

public class CountdownLatchExample {
    static class A extends Thread{
        //ReentrantLock lock = new ReentrantLock();
        int i = 0;
        public void add() {
            //lock.lock();
            try {
                i = i + 1;
                System.out.println("-------" + i);
            } finally {
                //lock.unlock();
            }
        }
    }
    public static void main(String[] args) throws InterruptedException {
        final int totalThread = 100;
        A a = new A();
        CountDownLatch countDownLatch = new CountDownLatch(totalThread);
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 100; i++) {
            executorService.execute( () -> {
                a.add();
                countDownLatch.countDown();
                System.out.println("count" + countDownLatch.getCount());
            });
        }
        countDownLatch.await();
        executorService.execute( () -> {
            System.out.println("**************8");
        });
        executorService.execute( () -> {
            System.out.println("**************8");
        });
        System.out.println("end");
        executorService.shutdown();
    }
}
